package com.qingchunlin.blogsystem.controller;

import com.qingchunlin.blogsystem.common.utils.JWTUtils;
import com.qingchunlin.blogsystem.entity.Blog;
import com.qingchunlin.blogsystem.entity.Result;
import com.qingchunlin.blogsystem.service.BlogService;
import com.qingchunlin.blogsystem.service.UserService;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private UserService userService;

    @RequestMapping("/getBlogList")
    public List<Blog> getBlogList() {
        //后期添加user_id的验证,只返回当前用户的blog_list
        return blogService.getBlogList();
    }

    @RequestMapping("/getBlogDetail")
    public Result getBLogDetail(Integer blogId) {
        if (blogId == null || blogId <= 0) {
            return Result.failure("无效blogId");
        }

        Blog blog = blogService.getBlogDetail(blogId);
        if (blog == null) {
            return Result.failure("无效blogId");
        }

        return Result.success(blog);
    }

    @RequestMapping("/publishBlog")
    public Result publishBLog(String title, String content, HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("user_token");
        Integer userId = JWTUtils.getUserIdByToken(token);
        if (userId == null || userId < 1) {
            return Result.failure("用户不存在", false);
        }
        //插入博客
        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(userId);
        blogService.insertBlog(blog);
        return Result.success(true);
    }

    @RequestMapping("/updateBlog")
    public Result updateBlog(Integer blogId, String title, String content, HttpServletRequest httpServletRequest) {
        boolean currentUserIsAuthor = userService.currentUserIsAuthor(blogId, httpServletRequest.getHeader("user_token"));

        if (!currentUserIsAuthor) {
           return Result.failure("不是当前用户操作", false);
        }

        if (blogId == null || blogId < 1) {
            return Result.failure("不正确的blogId", false);
        }

        Blog blog = new Blog();
        blog.setId(blogId);
        blog.setTitle(title);
        blog.setContent(content);
        blogService.updateBlog(blog);
        return Result.success(true);
    }

    @RequestMapping("/deleteBlog")
    public Result deleteBlog(Integer blogId, HttpServletRequest httpServletRequest) {
        boolean currentUserIsAuthor = userService.currentUserIsAuthor(blogId, httpServletRequest.getHeader("user_token"));

        if (!currentUserIsAuthor) {
            return Result.failure("不是当前用户操作", false);
        }

        if (blogId == null || blogId < 1) {
            return Result.failure("不正确的blogId", false);
        }

        blogService.deleteBlog(blogId);
        return Result.success(true);
    }

}
